﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using NewPanda.ViewModel;
using MusicPlayerLib.ViewModel;
using MusicPlayerLib.DataModel;
namespace NewPanda
{
    /// <summary>
    /// MusicPanel.xaml 的交互逻辑
    /// </summary>
    public partial class MusicPanel : System.Windows.Controls.UserControl
    {
        #region filed
        private MusicViewModel mvm = App.MVM;
        private MusicPlayerLib.MusicPlayer player;

        #endregion
        public MusicPanel()
        {
            InitMusicPlayer();
            this.DataContext = mvm;
            InitializeComponent();
           

            //设置各个组件的DataContext;
            //this.PlaybackControlGrid.DataContext = mvm.CurrentTrack;
            //this.PlaylistView.DataContext = mvm.CurrentPlaylist.Tracks;
        }


        private void InitMusicPlayer()
        {
            player = new MusicPlayerLib.MusicPlayer(mvm.CurrentPlaylist);
            player.UpdatePlayedTime += new DoWorkEventHandler(UpdatePlayedTime);
            
        }
        private void PreviousButton_Click(object sender, RoutedEventArgs e)
        {
            player.PlayPreviousTrack();
            mvm.UpdateCurrentTrack();
            PlaybackControlGrid.DataContext = mvm.CurrentTrack;
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            if (player.IsPaused)
            {
                player.ResumePlayback();
            }
            else
            {
                player.PausePlayback();
            }
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            player.PlayNextTrack();
            mvm.UpdateCurrentTrack();
            PlaybackControlGrid.DataContext = mvm.CurrentTrack;
        }


        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            player.StopPlayback();
            player.IsPaused = true;
        }

        private void MuteButton_Click(object sender, RoutedEventArgs e)
        {
            if (player.IsMute)
            {
                player.ReMute();
            }
            else
            {
                player.Mute();
            }
        }

        private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (player != null)
            {
                int volume = (int)(sender as Slider).Value;
                player.SetVal(volume);
            }
        }

        private void DurationSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (player != null&&sender.GetType().Equals(typeof(Slider)))
            {
                double duration = (sender as Slider).Value;
                //player.SeekTo(duration);
            }
        }


        private void UpdatePlayedTime(object sender,DoWorkEventArgs e)
        {
            while (mvm.CurrentTrack.PlayedTime != mvm.CurrentTrack.Duration)
            {
                double position = player.GetPosition();
                mvm.CurrentTrack.PlayedTime = position;
                System.Threading.Thread.Sleep(1000);
            }

            player.PlayNextTrack();
            //if(mvm.CurrentPlaylist.CIndex<(mvm.CurrentPlaylist.Tracks.Count-1))
            //{
               
            //}
            //else
            //{
            //    player.StopPlayback();
            //}
        }

        private void PlaylistView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            PlaylistViewModel list=(PlaylistViewModel)PlaylistView.DataContext;
            if(list!=mvm.CurrentPlaylist)
                mvm.CurrentPlaylist=list;

            int index = PlaylistView.SelectedIndex;
            mvm.CurrentPlaylist.CIndex = index;
            mvm.CurrentTrack = new AudioFileViewModel(mvm.CurrentPlaylist.GetCurrentTrack());
            PlaybackControlGrid.DataContext = mvm.CurrentTrack;
            player.StartPlayback();
        }

        private void PlaylistItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine(LibraryTreeView.SelectedItem);
            
            
            Debug.WriteLine("ff");
            PlaylistViewModel au = LibraryTreeView.SelectedItem as PlaylistViewModel;
            Debug.WriteLine(au.Title);
            mvm.CurrentPlaylist = au;
            
            
            //MessageBox.Show(sender.ToString()+":"+index);
        }

        private void PlaylistItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
           
        }

        private void LibraryTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

        }

        private void MeidaLibraryItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine(LibraryTreeView.SelectedItem);
            if (LibraryTreeView.SelectedItem+"" == "MusicPlayerLib.DataModel.AudioFile")
            {
                AudioFile au = LibraryTreeView.SelectedItem as AudioFile;
                Debug.WriteLine(au.Location);
            }
            else if (LibraryTreeView.SelectedItem + "" == "MusicPlayerLib.ViewModel.AlbumViewModel")
            {
                Debug.WriteLine("ee");

            }
        }

        private void ContextMenu_Click(object sender, MouseButtonEventArgs e)
        {

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
           
            playListInsert pli = new playListInsert();
            
            pli.ShowDialog();
            if (pli.comboBox1.SelectedItem != null)
            {
                Debug.WriteLine(LibraryTreeView.SelectedItem);
                
                MusicPlayerLib.ViewModel.PlaylistViewModel plvm = pli.comboBox1.SelectedItem as MusicPlayerLib.ViewModel.PlaylistViewModel;
                if (LibraryTreeView.SelectedItem + "" == "MusicPlayerLib.ViewModel.AlbumViewModel") 
                {
                    Debug.WriteLine("324");
                    AlbumViewModel avm = LibraryTreeView.SelectedItem as AlbumViewModel;
                    for (int i = 0; i < avm.Tracks.Count;i++ ) 
                    {
                        Debug.WriteLine(avm.Tracks[i]);
                        AudioFile au = avm.Tracks[i];
                        plvm.Tracks.Add(au);
                    }
                }
                else
                {
                    AudioFile au = LibraryTreeView.SelectedItem as AudioFile;
                    plvm.Tracks.Add(au);
                    Debug.WriteLine(plvm.Tracks.Count);
                }
            }
        }


        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void PlaylistItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            PlaylistViewModel curr = LibraryTreeView.SelectedItem as PlaylistViewModel;
            mvm.ListOfPlaylist.Remove(curr);
        }

        private void MeidaLibraryItem_MouseRightButtonDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

          //  TreeNode tn = TreeView.GetNodeAt(e.X, e.Y);
          //  if (tn != null) TreeView.SelectedNode = tn;   


 

        }

        private void addPl_Click(object sender, RoutedEventArgs e)
        {
            PlaylistViewModel newPlayList = new PlaylistViewModel();
            newPlayList.Title = newPLtext.Text;
            mvm.ListOfPlaylist.Add(newPlayList);
            newPLtext.Text = "";
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine(PlaylistView.SelectedItem);
            AudioFile af = PlaylistView.SelectedItem as AudioFile;
            mvm.CurrentPlaylist.Tracks.Remove(af);
        }
    }
}
